home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / lpc10 / tbdm.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  2KB  |  105 lines

  1. /***********************************************************************
  2. *
  3. *    TBDM Version 49
  4. *
  5. **********************************************************************
  6. *
  7. *  TURBO DIFMAG: Compute High Resolution Average Magnitude Difference Function
  8. *
  9. * Inputs:
  10. *  SPEECH - Low pass filtered speech
  11. *  LPITA  - Length of speech buffer
  12. *  TAU    - Table of lags
  13. *  LTAU   - Number of lag values to compute
  14. * Outputs:
  15. *  AMDF   - Average Magnitude Difference for each lag in TAU
  16. *  MINPTR - Index of minimum AMDF value
  17. *  MAXPTR - Index of maximum AMDF value within +/- 1/2 octave of min
  18. *  MINTAU - Lag corresponding to minimum AMDF value
  19. */
  20.  
  21. #include "lpcdefs.h"
  22.  
  23. tbdm( speech, tau, amdf, minptr, maxptr, mintau )
  24. float speech[], amdf[];
  25. int *minptr, *maxptr, *mintau, tau[];
  26. {
  27. int minamd;
  28. int i, ptr, ltau2, minp2, maxp2;
  29. float amdf2[6];
  30. static int tau2[6];
  31. static int count=0;
  32.  
  33.  
  34. /*    REAL SPEECH(LPITA+TAU(LTAU)), AMDF(LTAU), AMDF2(6)
  35.  
  36. /*   Compute full AMDF using log spaced lags, find coarse minimum    */
  37.  
  38. difmag(speech, tau, LTAU, tau[LTAU], amdf, minptr, maxptr );
  39.  
  40.  
  41. *mintau = tau[*minptr];
  42. minamd = amdf[*minptr];
  43.  
  44. /*   Build table containing all lags within +/- 3 of the AMDF minimum
  45. *    excluding all that have already been computed    */
  46.  
  47. ltau2 = 0;
  48. ptr = *minptr - 2;
  49. for(i=mmax(*mintau-3,41); i<=mmin(*mintau+3,tau[LTAU]); i++)    {
  50.     while( tau[ptr] < i )    {
  51.         ptr++;
  52.     }
  53.     if( tau[ptr] !=i) {
  54.         ltau2++;
  55.         tau2[ltau2-1] = i;
  56.     }
  57. }
  58.  
  59. /*   Compute AMDF of the new lags, if there are any, and choose one
  60. *    if it is better than the coarse minimum    */
  61.  
  62. if( ltau2 > 0 ) {
  63.     difmag(speech, &tau2[0]-1, ltau2, tau[LTAU], &amdf2[0]-1, &minp2, &maxp2);
  64.     
  65.     if( amdf2[minp2-1] < minamd ) {
  66.         *mintau = tau2[minp2-1];
  67.         minamd = amdf2[minp2-1];
  68.     }
  69. }
  70.  
  71. /*   Check one octave up, if there are any lags not yet computed    */
  72.  
  73. if( *mintau >= 80 ) {
  74.     i = *mintau*0.5;
  75.     if( (i & 1) == 0 ) {
  76.         ltau2 = 2;
  77.         tau2[0] = i-1;
  78.         tau2[1] = i+1;
  79.     }
  80.     else    {
  81.         ltau2 = 1;
  82.         tau2[0] = i;
  83.     }
  84.     difmag(speech, &tau2[0]-1, ltau2, tau[LTAU], &amdf2[0]-1, &minp2, &maxp2 );
  85.     
  86.     if( amdf2[minp2-1] < minamd ) {
  87.         *mintau = tau2[minp2-1];
  88.         minamd = amdf2[minp2-1];
  89.         *(minptr) -= 20;
  90.     }
  91. }
  92.  
  93. /*   Force minimum of the AMDF array to the high resolution minimum    */
  94.  
  95.     amdf[*minptr] = minamd;
  96.     
  97. /*   Find maximum of AMDF within 1/2 octave of minimum    */
  98.  
  99. *maxptr = mmax(*minptr-5,1);
  100. for(i=*maxptr+1; i<= mmin(*minptr+5,LTAU); i++)    {
  101.     if( amdf[i] > amdf[*maxptr]) *maxptr = i;
  102. }
  103.  
  104. }
  105.